;(function(undefined) {
    "use strict"
    var _global;

    function result(args,fn){
        var argsArr = Array.prototype.slice.call(args);
        if(argsArr.length > 0){
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }
    var  rightsList = {
        columnsInit:function(){
            return  [{
            	radio: true
            },{
                field: 'od',
                title: '序号',
                width:'5%',
                align:'center',
                formatter: function (value, row, index) {
                    return index + 1;
                }
            }, {
                field: 'roleName',
                title: '角色名称'
            }, {
                field: 'roleCode',
                title: '角色编码'
            },{
                field: 'option',
                title: '操作',
                width: 80,
                align: 'center',
                formatter: function (value, row, index) {
                    return '<a class="opt-icon" href="javascript:;" onclick="addRole(\'' + row + '\', \'' + value + '\');"><i class="glyphicon glyphicon-duplicate"></i></a>';
                }
            }]
        },
        usercolumnsInit:function(){
            return  [{
            	radio: true
            },{
                field: 'od',
                title: '序号',
                width:'5%',
                align:'center',
                formatter: function (value, row, index) {
                    return index + 1;
                }
            }, {
                field: 'userName',
                title: '用户名称'
            }, {
                field: 'userCode',
                title: '用户编码'
            },{
                field: 'option',
                title: '操作',
                width: 80,
                align: 'center',
                formatter: function (value, row, index) {
                    return '<a class="opt-icon" href="javascript:;" onclick="addUser(\'' + row + '\', \'' + value + '\');"><i class="glyphicon glyphicon-duplicate"></i></a>';
                }
            }]
        },
        gridInit:function(columns){
            return $("#roleTable").bootstrapTable({
                url:'',
                columns: columns,
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: 'true',
                pageNumber: 1,
                pageSize: 10,
                pageList: [
                    10, 30, 50, 100, 200
                ],
                height: ($('#rightCont').height())/2-60
            })

        },
        usergridInit:function(columns){
            return $("#userTable").bootstrapTable({
                url:'',
                columns: columns,
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: 'true',
                pageNumber: 1,
                pageSize: 10,
                pageList: [
                    10, 30, 50, 100, 200
                ],
                height: ($('#rightCont').height())/2-55
            })

        },
        dataInit:function(){
            return  [{
                roleName: '111',
                roleCode: '555464545',
            },{
                roleName: '111',
                roleCode: '555464545',
            },{
                roleName: '111',
                roleCode: '555464545',
            },{
                roleName: '111',
                roleCode: '555464545',
            },{
                roleName: '111',
                roleCode: '555464545',
            },{
                roleName: '111',
                roleCode: '555464545',
            },{
                roleName: '111',
                roleCode: '555464545',
            },{
                roleName: '111',
                roleCode: '555464545',
            },{
                roleName: '111',
                roleCode: '555464545',
            },{
                roleName: '111',
                roleCode: '555464545',
            },{
                roleName: '111',
                roleCode: '555464545',
            },{
                roleName: '111',
                roleCode: '555464545',
            }]
        },
        userdataInit:function(){
            return  [{
                userName: '李四',
                userCode: '555464545',
            },{
                userName: '王五',
                userCode: '555464545',
            },{
                userName: '李四',
                userCode: '555464545',
            },{
                userName: '王五',
                userCode: '555464545',
            },{
                userName: '李四',
                userCode: '555464545',
            },{
                userName: '王五',
                userCode: '555464545',
            },{
                userName: '李四',
                userCode: '555464545',
            },{
                userName: '王五',
                userCode: '555464545',
            },{
                userName: '李四',
                userCode: '555464545',
            },{
                userName: '王五',
                userCode: '555464545',
            },{
                userName: '李四',
                userCode: '555464545',
            },{
                userName: '王五',
                userCode: '555464545',
            }]
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function(){ return this || (0, eval)('this'); }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = rightsList;
    } else if (typeof define === "function" && define.amd) {
        define(function(){return rightsList;});
    } else {
        !('plugin' in _global) && (_global.rightsList = rightsList);
    }
}());
$(function () {
    //tree
    var DataSource = [{
        "DataSourceID": 1,
        "rights": "cccs0002123",
        "expanded": "true",
        children: [{
            "DataSourceID": 2,
            "rights": "cccs0002123",
            "expanded": "true",
            children: [{
                "DataSourceID": 3,
                "rights": "cccs0002123",
                "expanded": "true",
            },{
                "DataSourceID": 4,
                "rights": "cccs0002123"
            }]
        },{
            "DataSourceID": 5,
            "rights": "cccs0002123"
        }]
    },{
        "DataSourceID": 6,
        "rights": "cccs0002123",
        "expanded": "true",
        children: [{
            "DataSourceID": 7,
            "rights": "cccs0002123"
        },{
            "DataSourceID": 8,
            "rights": "cccs0002123"
        }]
    }];
    var source ={
        dataType: "json",
        dataFields: [{
            name: "rights",
            type: "string"
        },{
            name: 'children',
            type: 'array'
        },{
            name: 'expanded',
            type: 'bool'
        }],
        hierarchy: {
            root: 'children'
        },
        id: 'DataSourceID',
        localData: DataSource
    };
    var dataAdapter = new $.jqx.dataAdapter(source);
    var iHeight=$('#rightCont').height()-70;
    // create Tree Grid
    $("#rightsTable").jqxTreeGrid({
        source: dataAdapter,
        width: '100%',
        altRows: true,
        selectionMode: "singleRow",
        height: iHeight,
        theme: 'bootstrap',
        showToolbar: false,
        checkboxes: true,
        hierarchicalCheckboxes: true,
        columns: [{
            text: "菜单",
            align: "center",
            dataField: "rights"
        }]
    });
    //渲染表格
    var columns = rightsList.columnsInit();
    var table = rightsList.gridInit(columns);
    var datas = rightsList.dataInit();
    var usercolumns = rightsList.usercolumnsInit();
    var usertable = rightsList.usergridInit(usercolumns);
    var userdatas = rightsList.userdataInit();
    $("#roleTable").bootstrapTable("append",datas);
    $('#roleTable').bootstrapTable('resetView');
    $("#userTable").bootstrapTable("append",userdatas);
    $('#userTable').bootstrapTable('resetView');
    //动态计算表格高度
    $(window).resize(function () {
        $('#roleTable , #userTable').bootstrapTable('resetView', {
            height: ($('#rightCont').height())/2-55
        });
        $("#rightsTable").jqxTreeGrid({
            height: $('#rightCont').height()-70
        });
    });
    $('#roleName').focus();
    //点击角色表格刷新用户表格和权限表格
	$("#roleTable").on('click-row.bs.table',function(row,$element){
		$("#userTable").bootstrapTable('refresh');
		$("#rightsTable").jqxTreeGrid('updateBoundData');
	});
	$("#userTable").on('click-row.bs.table',function(row,$element){
		$("#roleTable").bootstrapTable('refresh');
		$("#rightsTable").jqxTreeGrid('updateBoundData');
	});
	//搜索
	$('#serachBtn').on('click',function(){
		$('#roleTable').bootstrapTable('refresh');
	})
	$('#serachBtn1').on('click',function(){
		$('#userTable').bootstrapTable('refresh');
	})
	//重置
	$('#searchReset').on('click',function(){
		$('#searchForm input').val('');
		$('#roleTable').bootstrapTable('refresh');
	})
	$('#searchReset1').on('click',function(){
		$('#searchForm1 input').val('');
		$('#userTable').bootstrapTable('refresh');
	})
	console.log((".sidebar-toggle"));
})
function addRole(row) {
    //新增角色
    layer.open({
        move: false,
        type: 2,
        title: row.roleName +'操作',
        area: ['900px', '500px'],
        content: 'j_rights_addRole.html',
        btn: ['确定','关闭'],
        btnAlign: 'c',
        yes: function (index, layero) {
            layer.close(index);
        }, cancel: function (index) {
            layer.close(index);
        }
    });
}
function addUser(row) {
    //新增角色
    layer.open({
        move: false,
        type: 2,
        title: row.userName +'操作',
        area: ['900px', '500px'],
        content: 'j_rights_addUser.html',
        btn: ['确定','关闭'],
        btnAlign: 'c',
        yes: function (index, layero) {
            layer.close(index);
        }, cancel: function (index) {
            layer.close(index);
        }
    });
}